﻿using Castle.Core.Logging;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace com.ytocargo.Application
{
    public class MqHelper
    {
        public static void SendMq(string exChange,string routingKey,string message,ILogger logger)
        {
            try
            {
                var factory = new ConnectionFactory();
                factory.HostName = "202.43.148.92";
                factory.Port = 5672;
                factory.VirtualHost = "AirLines";
                factory.UserName = "HYT";
                factory.Password = "HYT2020!";
                factory.AutomaticRecoveryEnabled = true;
                factory.NetworkRecoveryInterval = new TimeSpan(5000L);
                factory.ContinuationTimeout = new TimeSpan(5000L);

                //创建一个新的连接
                using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())//创建一个频道
                {

                    //一次只发送一个，处理完成一个再获取下一个
                    //channel.BasicQos(1);

                    var messageBody = Encoding.UTF8.GetBytes(message);

                    channel.BasicPublish(exChange, routingKey, null, messageBody);
                    Console.WriteLine("" + message);
                    //logger.Info(message);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
            }
        }
    }
}
